// pages/DingDanXiangDaiShouHuo/DingDanXiangDaiShouHuo.js
const Monitor = require('../../utils/monitor.js').Monitor
const util = require('../../utils/util.js')
Page(Monitor.hookApp({
  data: {
    waysArr:[],
    psWay: '',
    tijiao: `${getApp().globalData.imgUrl}jsy-newshop提交成功.svg`,
    qingdan: `${getApp().globalData.imgUrl}jsy-newshop清单.svg`,
    imgUrls: getApp().globalData.imgUrls,
    // markers: [{
    //   iconPath: `${getApp().globalData.imgUrl}jsynewshop家.svg`,
    //   id: 0,
    //   latitude: 45.75997,
    //   longitude: 126.668788,
    //   width: 50,
    //   height: 50
    // }, {
    //   iconPath: `${getApp().globalData.imgUrl}jsynewshop地图.svg`,
    //   id: 1,
    //   latitude: 49.75997,
    //   longitude: 126.668790,
    //   width: 50,
    //   height: 50
    // }]

    hasMarkers: true,
    markers: [{
        id: 0,
        latitude: 45.75997,
        longitude: 126.668788,
        iconPath: `${getApp().globalData.imgUrl}jsynewshop家.svg`,
        width: 32,
        height: 32
      },
      {
        id: 1,
        latitude: 49.768897,
        longitude: 126.668788,
        iconPath: `${getApp().globalData.imgUrl}jsynewshop地图.svg`,
        width: 32,
        height: 32,
        callout: {
          content: " 0 ",
          textAlign: 'left',
          color: "#000000",
          fontSize: 18,
          borderRadius: 10,
          bgColor: "#ffffff",
          padding: 5,
          display: "ALWAYS"
        },
      }

    ]
  },


  onLoad: function(options) {
    this.setData({
      distributiontype: options.distributiontype,
      order_id: options.order_id
    })
    if (options.idx == 0) {
      this.setData({
        psWay: '商家配送'
      })
    } else {
      this.setData({
        psWay: '同城限时达'
      })
    }
  },
  onShow() {
    this.getInfo()

  },
  copyText(e) { //点击复制
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  setMap(boolean) {
    if (boolean){
      if (this.data.hasMarkers) {
        this.mapCtx = wx.createMapContext('myMap');
        this.mapCtx.includePoints({
          padding: [10],
          points: [{
            latitude: this.data.markers[0].latitude,
            longitude: this.data.markers[0].longitude,
          }, {
            latitude: this.data.markers[1].latitude,
            longitude: this.data.markers[1].longitude,
          }]

        })
      }
    }else{
      if (this.data.hasMarkers) {
        this.mapCtx = wx.createMapContext('myMap');
        this.mapCtx.includePoints({
          padding: [10],
          points: [{
            latitude: this.data.markers[0].latitude,
            longitude: this.data.markers[0].longitude,
          }]
        })
      }
    }
    
  },

  specifications(trg) { //商品规格转换
    let len = trg.length
    for (let i = 0; i < len; i++) {
      let size = ''
      let str = `${trg[i].sku_type}`
      trg[i].sku_type = JSON.parse(str)
      for (let j = 0; j < trg[i].sku_type.length; j++) {
        for (let prop in trg[i].sku_type[j]) {
          size += trg[i].sku_type[j][prop] + " / "
        }
      }
      trg[i].size = size
      trg[i].size = trg[i].size.substring(0, trg[i].size.length - 3)
    }
    return trg
  },
  showSure(e) {
    console.log(e.currentTarget.dataset.orderid)
    wx.showModal({
      title: '确认收货',
      content: '请您再次确认是否收到货了！',
      success(res) {
        if (res.confirm) {
          util.API('pyapi/Confirmation_receipt', {
            order_id: e.currentTarget.dataset.orderid
          }, 'application/x-www-form-urlencoded').then(res => {
            wx.navigateTo({
              url: `../DingDanXiangYiWanChengSJPS/DingDanXiangYiWanChengSJPS?order_id=${e.currentTarget.dataset.orderid}&distributiontype=${e.currentTarget.dataset.distributiontype}`
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
        
      }
    })
  },

  getInfo() {
    util.API('pyapi/oder_if', {
      order_id: this.data.order_id
    }, 'application/x-www-form-urlencoded').then(res => {
      let {
        code,
        address,
        address_info,
        data,
        distribution_name,
        orders_goods_list,
        info
      } = res
      if (code == 200) {
        let date = util.getTimeDian(data.ctime)
        data.ctime = `${date.y}.${date.m}.${date.d} ${date.h}:${date.i}:${date.s}`
        orders_goods_list = this.specifications(orders_goods_list)
        let waysArr = this.setWaysArr([], this.data.distributiontype, JSON.stringify(distribution_name))
        this.setData({
          waysArr,
          address,
          address_info,
          data,
          orders_goods_list,
          info
        })

        // 需要改
        // info.trail = [{
        //   latitude:'1',
        //   longitude:'2'
        // }]
        // info.trail[0].latitude = 45.75987 
        // info.trail[0].longitude = 126.678786 

        // console.log(info)
        // 需要改

        if (this.data.distributiontype == 2) {
          let markers = this.data.markers
          markers[0].latitude = this.data.address.latitude
          markers[0].longitude = this.data.address.longitude

          if (info.trail) {
            markers[1].latitude = info.trail ? info.trail[0].latitude : ''
            markers[1].longitude = info.trail ? info.trail[0].longitude : ''
            markers[1].callout.content = `骑手距您${this.getdistances(markers[0].latitude, markers[0].longitude, markers[1].latitude, markers[1].longitude)}`
            this.setData({
              markers
            })
            console.log(this.data.markers)
            this.setMap(true)
          } else {
            this.setData({
              markers
            })
            this.setMap(false)
          }

          // 需要改



        }
      }
    })
  },
  // let waysArr = this.setWaysArr([], this.data.distributiontype, JSON.stringify(distribution_name))
  setWaysArr(waysArr, distribution_type, distribution_name) {  // 设置自定义配送信息
    distribution_type = distribution_type.split(',');
    distribution_name = JSON.parse(distribution_name)
    let len = distribution_type.length
    for (let i = 0; i < len; i++) {
      waysArr.push(distribution_name[distribution_type[i]])
    }
    return waysArr
  },
  deg2rad(degrees) {
    return (Math.PI * degrees) / 180;
  },
  getdistances(lng1, lat1, lng2, lat2) {
    // 将角度转为狐度
    let radLat1 = this.deg2rad(lat1); //deg2rad()函数将角度转换为弧度
    let radLat2 = this.deg2rad(lat2);
    let radLng1 = this.deg2rad(lng1);
    let radLng2 = this.deg2rad(lng2);
    let a = radLat1 - radLat2;
    let b = radLng1 - radLng2;
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))) * 6378.137 * 1000;
    //    var_dump($lng1);
    //    var_dump($lat1);
    //    var_dump($lng2);
    //    var_dump($lat2);
    //    dd($s);
    if (s / 1000 < 1) {
      var distance = `${Math.round(s, 2)}m`
    } else {
      var distance = `${Math.round((s / 1000), 2)}km`
    }
    return distance;
  }
}))